import request from '@/utils/request'

let mockUserList = [
  { id: 1, username: 'admin', name: '管理员', role: ['admin'], status: '启用' },
  { id: 2, username: 'user1', name: '张三', role: ['operator'], status: '启用' }
];

export function login(data) {
  return request({
    url: '/system/users/login',
    method: 'post',
    data
  })
}

export function getInfo() {
  return request({
    url: '/system/users/info',
    method: 'get'
  })
}

export function logout() {
  return request({
    url: '/system/users/logout',
    method: 'post'
  })
}

export function getUserList(params) {
  return request({
    url: '/system/users',
    method: 'get',
    params
  })
}

export function getUsers() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockUserList.length, items: mockUserList }
      });
    }, 300);
  });
}

export function addUser(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const newItem = { ...data };
      mockUserList.unshift(newItem);
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function updateUser(id, data) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockUserList = mockUserList.map(item => item.id === id ? { ...item, ...data } : item);
      resolve({ code: 20000, data: { ...data, id } });
    }, 300);
  });
}

export function deleteUser(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      mockUserList = mockUserList.filter(item => item.id !== id);
      resolve({ code: 20000 });
    }, 300);
  });
} 